package com.dongni.tools;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public final class ToastUtils {

    private static volatile Toast toast;
    private static LayoutInflater sInflater;

    private ToastUtils() {
    }

    private static void getToast(Context context) {
        if (toast == null) {
            synchronized (ToastUtils.class) {
                if (toast == null) {
                    toast = new Toast(context);
                    View view = Toast.makeText(context, "", Toast.LENGTH_SHORT).getView();
                    toast.setView(view);
                }
            }
        }
    }

    private static void getToastView(Context context) {
        if (toast == null) {
            synchronized (ToastUtils.class) {
                if (toast == null) {
                    toast = new Toast(context);
                    sInflater = LayoutInflater.from(context);
                }
            }
        }
    }

    public static void showShortToast(Context context, CharSequence msg) {
        showToast(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(Context context, int resId) {
        showToast(context.getApplicationContext(), resId, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(Context context, CharSequence msg) {
        showToast(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(Context context, int resId) {
        showToast(context.getApplicationContext(), resId, Toast.LENGTH_LONG);
    }

    private static void showToast(Context context, CharSequence msg, int duration) {
        cancelToast();
        try {
            getToast(context);
            toast.setText(msg);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showToast(Context context, int resId, int duration) {
        cancelToast();
        try {
            if (resId == 0) {
                return;
            }
            getToast(context);
            toast.setText(resId);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showShortToastView(Context context, CharSequence msg) {
        showToastView(context, msg, Toast.LENGTH_SHORT);
    }

    public static void showShortToastView(Context context, int resId) {
        showToastView(context, resId, Toast.LENGTH_SHORT);
    }

    public static void showLongToastView(Context context, CharSequence msg) {
        showToastView(context, msg, Toast.LENGTH_LONG);
    }

    public static void showLongToastView(Context context, int resId) {
        showToastView(context, resId, Toast.LENGTH_LONG);
    }

    public static void showToastView(Context context, CharSequence msg, int duration) {
        cancelToast();
        try {
            getToastView(context);
            View view = sInflater.inflate(R.layout.toast_layout, null);
            TextView msgTv = view.findViewById(R.id.toast_msg);
            msgTv.setText(msg);
            toast.setView(view);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToastView(Context context, int resId, int duration) {
        cancelToast();
        try {
            getToastView(context);
            View view = sInflater.inflate(R.layout.toast_layout, null);
            TextView msg = view.findViewById(R.id.toast_msg);
            msg.setText(resId);
            toast.setView(view);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }
}
